﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Text;

namespace IHISCETeim.Web.MachineSys.FXModel
{
    public partial class ReportIncome : PageBase
    {
        protected override string DWZRef
        {
            get
            {
                return "ReportIncome";
            }
        }

        protected int ReportType
        {
            get
            {
                return GetRequestValue<int>("rt", 1);
            }
        }

        protected string AliasName
        {
            get
            { 
                return GetRequestValue<string>("AliasName", "");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = 0;
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            Response.AddHeader("pragma", "no-cache");
            Response.CacheControl = "no-cache";

            switch (ReportType)
            {
                case 1:
                    DataBind1();
                    break;
                case 2:
                    DataBind2();
                    break;
                case 3:
                    DataBind3();
                    break;
            }
        }
        #region 1.经销商逾期率
        
        private DataTable sumdt = null;
        private void DataBind1()
        {
            DataTableCollection dtc = FXFinanceLeaseController.ReportIncome();

            sumdt = dtc[1];

            this.gv1.DataSource = dtc[0];
            this.gv1.DataBind();

            
        }

        protected string getsum(string columnname)
        {
            return FormatDecimalToStrings(sumdt.Rows[0][columnname], "N");
        }
        #endregion

        #region 2.整体逾期率趋势


        private DataTable _report2dts = null;
        private DataTable report2dts
        {
            get
            {
                if (_report2dts == null)
                {

                    _report2dts = FXFinanceLeaseController.ReportIncomeByAliasName(AliasName);
                }
                return _report2dts;
            }
        }

        private void DataBind2()
        {
            this.gv2.DataSource = report2dts;
            this.gv2.DataBind();
        }

        protected string GetReport2JSCategories()
        {
            DataTable dtcl = report2dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int colindex = 0;
            foreach (DataRow dr in dtcl.Rows)
            {               
                sb.AppendFormat("'{0}'", dr["年份"]);
                if (colindex < dtcl.Rows.Count-1)
                {
                    sb.AppendFormat(",");
                }
                colindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport2JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report2dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");
            string str直接 = "";
            int rowindex = 0;
            str直接 = "{name: '净收入',data:[";
            foreach (DataRow dr in dtmain.Rows)
            {

                str直接 += string.Format("[{0},{1}]", rowindex, dr["净收入"]);
                if (rowindex < dtmain.Rows.Count - 1)
                {
                    str直接 += ",";
                }

                rowindex++;                
            }

            str直接 += "]}";
           
            sb.AppendLine(str直接);



            sb.AppendLine("]");
            return sb.ToString();
        }

        #endregion


        #region 3.管理费收益分析之净收入年对比

        private DataTable _report3dts = null;
        private DataTable report3dts
        {
            get
            {
                if (_report3dts == null)
                {
                    _report3dts = FXFinanceLeaseController.ReportIncomeLMFeeIncome();

                    //合计行处理
                    DataTable dt = _report3dts.Copy();
                    
                    //合计列处理
                    DataColumn sumdc = new DataColumn("合计", typeof(decimal));
                    dt.Columns.Add(sumdc);
                    foreach (DataRow dr in dt.Rows)
                    {
                        decimal sum = 0;
                        foreach (DataColumn dc in dt.Columns)
                        {
                            if (dc.ColumnName == "经销商" || dc.ColumnName == "合计")
                                continue;
                            sum += dr[dc.ColumnName] is DBNull ? 0 : Convert.ToDecimal(dr[dc.ColumnName]);
                        }
                        dr["合计"] = sum;
                    }

                    DataTable dt2 = dt.Copy();
                    DataView dv = dt2.DefaultView;
                    dv.Sort = " 合计 desc";

                    dt = dv.ToTable();


                    DataRow drnew = dt.NewRow();
                    //合计
                    foreach (DataColumn dc in dt.Columns)
                    {
                        drnew[dc.ColumnName] = 0;
                    }
                    foreach (DataColumn dc in dt.Columns)
                    {
                        if (dc.ColumnName == "经销商")
                            continue;
                        foreach (DataRow dr in dt.Rows)
                        {
                            drnew[dc.ColumnName] = Convert.ToDecimal(drnew[dc.ColumnName]) + (dr[dc.ColumnName] is DBNull ? 0 : Convert.ToDecimal(dr[dc.ColumnName]));
                        }
                    }
                    // drnew["占比"] = "100%";
                    drnew["经销商"] = "合计";
                    dt.Rows.Add(drnew);


                    _report3dts = dt;
                }
                return _report3dts;
            }
        }

        private void DataBind3()
        {
          //  this.gv3.DataSource = report3dts;
           // this.gv3.DataBind();
            
        }

        protected string getdt3()
        {
            #region HTML输出
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(" <table class=\"table\" width=\"98%\" ><thead>");
            sb.AppendFormat("<tr><th rowspan=2></th><th rowspan=2>经销商</th><TH colspan=\"{0}\">净收入</TH><th rowspan=2>合计</th></tr>", report3dts.Columns.Count - 2);
            sb.AppendLine("<tr> ");
            foreach (DataColumn dc in report3dts.Columns)
            {
                if (dc.ColumnName == "经销商" || dc.ColumnName == "合计")
                    continue;
                sb.AppendFormat("<th>{0}</th>", dc.ColumnName);
            }
            sb.AppendLine("</tr> ");
            sb.AppendLine("</thead>");
            sb.AppendLine("<tbody>");
            foreach (DataRow dr in report3dts.Rows)
            {
                int rowsindex = tableRowIndex++;
                sb.AppendFormat("<tr class=\"{1}\"><td>{0}</td> ", rowsindex == report3dts.Rows.Count ? "" : (rowsindex).ToString(), rowsindex == report3dts.Rows.Count ? "SumRow" : rowsindex.ToString());
                foreach (DataColumn dc in report3dts.Columns)
                {
                    sb.AppendFormat("<td>{0}</td>", FormatDecimalToStrings( dr[dc.ColumnName],"N"));
                }
                sb.AppendLine("</tr> ");
            }
            sb.AppendLine("</tbody>");
            sb.AppendLine("</table>");
            #endregion

            return sb.ToString();
        }
        #endregion
    }
}